package com.effectiv.gooruda.rest.service.impl;

import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.twitter.api.CursoredList;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.TwitterProfile;
import org.springframework.stereotype.Component;

import com.effectiv.gooruda.rest.ext.ExtResponse;
import com.effectiv.gooruda.rest.service.api.TwitterBlockService;

@Component
public class TwitterBlockServiceImpl extends TwitterBlockService {
	@Autowired
	private SocialResolver resolver;

	public Response block(String provider, long userId) {
		Twitter twitter = resolver.resolveTwitter(provider);

		TwitterProfile profile = twitter.blockOperations().block(userId);

		ExtResponse res = new ExtResponse();
		res.setValue(profile);
		res.setMessage("Blocked user with id - " + userId);

		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res,
				ExtResponse.class);

		ResponseBuilder builder = Response.ok(entity);

		return builder.build();
	}

	public Response block(String provider, String screenName) {
		Twitter twitter = resolver.resolveTwitter(provider);
		TwitterProfile profile = twitter.blockOperations().block(screenName);
		ExtResponse res = new ExtResponse();
		res.setValue(profile);
		res.setMessage("Blocked user with screen name - " + screenName);

		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res,
				ExtResponse.class);

		ResponseBuilder builder = Response.ok(entity);

		return builder.build();
	}

	public Response getBlockedUsers(String provider) {
		Twitter twitter = resolver.resolveTwitter(provider);
		CursoredList<TwitterProfile> profiles = twitter.blockOperations()
				.getBlockedUsers();
		ExtResponse res = new ExtResponse();
		res.setValue(profiles);
		
		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res, ExtResponse.class);
		
		ResponseBuilder builder = Response.ok(entity);
		
		return builder.build();
	}

	public Response unblock(String provider, long userId) {
		Twitter twitter = resolver.resolveTwitter(provider);
		TwitterProfile profile = twitter.blockOperations().unblock(userId);
		ExtResponse res = new ExtResponse();
		res.setValue(profile);
		res.setMessage("Unblocked user with id - " + userId);

		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res,
				ExtResponse.class);

		ResponseBuilder builder = Response.ok(entity);

		return builder.build();
	}

	public Response unblock(String provider, String screenName) {
		Twitter twitter = resolver.resolveTwitter(provider);
		TwitterProfile profile = twitter.blockOperations().unblock(screenName);
		ExtResponse res = new ExtResponse();
		res.setValue(profile);
		res.setMessage("Unblocked user with screen name - " + screenName);

		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res,
				ExtResponse.class);

		ResponseBuilder builder = Response.ok(entity);

		return builder.build();
	}

}
